<?
function ricercaAnag($sRic, &$aAnagr) {
	global $objDB, $objSess;
	$aAnagr = array();
	$sRic = trim($sRic);
	if (substr($sRic,-1)==')') {
		$pos = strpos($sRic,'(');
		if ($pos!==false && is_numeric(substr($sRic,$pos+1,-1))) {
			$idAnag = substr($sRic,$pos+1,-1);
			$sSql = "SELECT intestaz1, id_anag FROM ovf_anagrafiche WHERE id_anag = $idAnag ";
			$objDB->query($sSql);
			$Anagr = array();
			$aR = $objDB->fetch();
			if($aR) {
				$aAnagr[$aR['id_anag']] = $aR['intestaz1'];
			}
			if (count($aAnagr)==0) return "anagrafica non trovata";
			return "";
		}
	}
	$sRic = preg_replace("/[^a-z0-9]/", ' ', strtolower($sRic));
	$aRic = explode(' ',$sRic);
	foreach($aRic as $kX => $sX) {
		if (strlen(trim($sX)) < 3) unset($aRic[$kX]);
	}
	if (count($aRic)==0) return "caratteri insufficienti per ricerca";
	$sSql = "SELECT intestaz1, id_anag FROM ovf_anagrafiche "
		." WHERE ";
	$sAnd = '';
	foreach($aRic as $kX => $sX) {
		$sSql .= $sAnd." intestaz1 like '%".addslashes($sX)."%' ";
		$sAnd = ' AND ';
	}
	$objDB->query($sSql);
	$Anagr = array();
	while($aR = $objDB->fetch()) {
		$aAnagr[$aR['id_anag']] = $aR['intestaz1'];
	}
	if (count($aAnagr)==0) return "anagrafica non trovata";
	if (count($aAnagr)>1) return "piu' di un'anagrafica";
	return "";
}
